package com.hxd.otc.module.my.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.api.Constant;
import com.hxd.otc.base.LazyFragment;
import com.hxd.otc.common.enums.BankType;
import com.hxd.otc.module.login.activity.PhoneCodeActivity;
import com.hxd.otc.module.my.contract.AddOtherPayContract;
import com.hxd.otc.module.my.presenter.AddOtherPayPresenter;
import com.hxd.otc.widget.EditInfoInputView;

import butterknife.BindView;
import butterknife.OnClick;

import static android.app.Activity.RESULT_OK;
import static com.hxd.otc.module.my.activity.AddPayActivity.EXTRA_BANK_TYPE;

/**
 * Created by lzy on 2018/6/8 0008.
 * description ${添加其他付款方式：paypal，西联付款，swify国际付款}
 */
public class AddOtherPayFragment extends LazyFragment<AddOtherPayPresenter> implements AddOtherPayContract.View {

    @BindView(R.id.editInfoInputView_name)
    EditInfoInputView mEditInfoInputViewName;
    @BindView(R.id.editInfoInputView_account)
    EditInfoInputView mEditInfoInputViewAccount;
    @BindView(R.id.editInfoInputView_for_note)
    EditInfoInputView mEditInfoInputViewForNote;
    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;
    @BindView(R.id.tv_reminder)
    TextView mTvReminder;
    @BindView(R.id.tv_choose_area)
    TextView tvChooseArea;

    private BankType mBankType;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() == null) return;
        mBankType = (BankType) getArguments().getSerializable(EXTRA_BANK_TYPE);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_add_other_pay;
    }

    @Override
    protected AddOtherPayPresenter createPresenter() {
        return new AddOtherPayPresenter(this);
    }

    public static AddOtherPayFragment getInstance() {
        return new AddOtherPayFragment();
    }

    @Override
    protected void initView() {
        mEditInfoInputViewAccount.setInputHint(String.format(getString(R.string.my_please_input_account), mBankType.getType() == 3 ? mBankType.getBankName() : ""));
        mTvReminder.setText(String.format(getString(R.string.my_add_other_pay_reminder), mBankType.getBankName()));
    }

    @OnClick(R.id.tv_confirm)
    public void onClick() {
        if (TextUtils.isEmpty(tvChooseArea.getText().toString())) {
            showToast(getString(R.string.choose_country_or_region));
            return;
        }
        String name = mEditInfoInputViewName.getText();
        if (TextUtils.isEmpty(name)) {
            showToast(mEditInfoInputViewName.getHint());
            return;
        }
        String account = mEditInfoInputViewAccount.getText();
        if (TextUtils.isEmpty(account)) {
            showToast(mEditInfoInputViewAccount.getHint());
            return;
        }

        String remark = mEditInfoInputViewForNote.getText();

        mPresenter.addOtherPay(tvChooseArea.getText().toString().trim(), name, account, "", remark, mBankType);
    }

    @OnClick(R.id.ll_choose_area)
    public void onClickArea() {
        Bundle bundle = new Bundle();
        bundle.putBoolean("isAddBank", true);
        $startActivityForResult(PhoneCodeActivity.class, 123, bundle, false);
    }

    @Override
    protected void initEvent() {
    }

    @Override
    public void addOtherPaySuccess(String msg) {
        showToast(msg);
        getActivity().setResult(Activity.RESULT_OK);
        getActivity().finish();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == 123) {
            tvChooseArea.setText(data.getStringExtra(Constant.Login.PHONE_CODE_DATA));
        }
    }
}
